package de.rj.urwalking.ui.activities;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;

import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.MenuItem;

import de.rj.urwalking.R;
import de.rj.urwalking.util.PrefUtil;

@SuppressWarnings("deprecation")
// using the deprecated PreferenceActivity without Fragments since we just need
// a few simple settings (for now)
public class GeneralPreferenceActivity extends SherlockPreferenceActivity implements
		OnSharedPreferenceChangeListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initUI();

		addPreferencesFromResource(R.xml.general_preferences);

		initSummaries();

	}

	private void initUI() {
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setTitle(getResources().getString(R.string.preferences));
	}

	private void initSummaries() {

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

		if (prefs.getBoolean(getString(R.string.key_pref_test_mode), false)) {
			findPreference(getString(R.string.key_pref_example_location)).setEnabled(true);
		} else {
			findPreference(getString(R.string.key_pref_example_location)).setEnabled(false);
		}

		String exampleLocation = prefs.getString(getString(R.string.key_pref_example_location),

		getString(R.string.default_location));
		findPreference(getString(R.string.key_pref_example_location)).setSummary(exampleLocation);
		
		String exampleXML = prefs.getString(getString(R.string.key_pref_xml),

		getString(R.string.default_xml));
		findPreference(getString(R.string.key_pref_xml)).setSummary(exampleXML);

	}

	@Override
	protected void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
				this);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go back
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

		if (key.equals(getString(R.string.key_pref_test_mode))) {

			findPreference(getString(R.string.key_pref_example_location)).setEnabled(
					sharedPreferences.getBoolean(key, false));
			
			PrefUtil.clearCredentials(this);

		} else if (key.equals(getString(R.string.key_pref_example_location))) {

			findPreference(key).setSummary(
					sharedPreferences.getString(key, getString(R.string.default_location)));
		}
		else if(key.equals(getString(R.string.key_pref_xml)))
		{
			findPreference(key).setSummary(
					sharedPreferences.getString(key, getString(R.string.default_xml)));
		}
	}

}
